﻿using System;
using WiMo.Games;
using WiMo.Games.Drawables;
using WiMo.Games.StandardInput;
using WiMo.Games.Inputs;
using WiMo.Games.Audio;

namespace InputController
{
    public class Game : IGame
    {
        ISpriteImage _up;
        ISpriteImage _down;
        ISpriteImage _left;
        ISpriteImage _right;
        IGameAudio _blip;

        private string _rightMessage = string.Empty;

        public Game(IGameEngine engine)
        {
            Engine = engine;                                    
        }

        public void Initialize()
        {
            Engine.EnableDiagnostics = true;
            Engine.DiagnosticsFontColor = Color.White;

            Engine.ContentManager.ContentLocation = "Content";
            Engine.InputManager.UseAccelerometerAsThumbPad = true;
            Engine.Display.BackgroundColor = Color.CornFlowerBlue;
        }

        public void BeginRun(){ }

        public void CreateMenus()
        {
            Engine.InputManager.SoftMenuFontColor = Color.White;
            Engine.InputManager.RightSoftButton = "Exit";
            Engine.InputManager.LeftSoftButton = "Press/Hold Me";
        }

        public void LoadContent()
        {
            _up = Engine.ContentManager.LoadImage("Up");
            _up.Position = new PointF(120, 100);
            _up.ColorKey = Color.White;
            _up.IsVisible = false;
            
            _down = Engine.ContentManager.LoadImage("Down");
            _down.Position = new PointF(120, 200);
            _down.ColorKey = Color.White;
            _down.IsVisible = false;

            _right = Engine.ContentManager.LoadImage("Right");
            _right.Position = new PointF(170, 150);
            _right.ColorKey = Color.White;
            _right.IsVisible = false;

            _left = Engine.ContentManager.LoadImage("Left");
            _left.Position = new PointF(70, 150);
            _left.ColorKey = Color.White;
            _left.IsVisible = false;

            _blip = Engine.ContentManager.LoadAudio("Blip");
        }

        Vector3 _lastGVector;
        IInputManager _lastInput;

        public void Update(GameTime gameTime)
        {
            if(Engine.InputManager.SupportAccelerometer)
                _lastGVector = Engine.InputManager.GVector;

            if (Engine.InputManager.WasPressed(_lastInput, Keys.Left))
            {
                _blip.Play();
                Engine.Vibrate(5);
            }

            if (Engine.InputManager.WasPressed(_lastInput, Keys.Right))
            {
                _blip.Play();
                Engine.Vibrate(5);
            }

            if (Engine.InputManager.WasPressed(_lastInput, Keys.Up))
            {
                _blip.Play();
                Engine.Vibrate(5);
            }

            if (Engine.InputManager.WasPressed(_lastInput, Keys.Down))
            {
                _blip.Play();
                Engine.Vibrate(5);
            }

            _up.IsVisible = (Engine.InputManager.IsKeyDown(Keys.Up));
            _down.IsVisible = (Engine.InputManager.IsKeyDown(Keys.Down));
            _left.IsVisible = (Engine.InputManager.IsKeyDown(Keys.Left));
            _right.IsVisible = (Engine.InputManager.IsKeyDown(Keys.Right));

            if (Engine.InputManager.IsKeyDown(Keys.RightSoftButton))
                Engine.Exit();

            if (Engine.InputManager.IsPressed(_lastInput, Keys.LeftSoftButton, 750))
                _rightMessage = (!string.IsNullOrEmpty(_rightMessage)) ? string.Empty : "Key Repeat/Debounce";

            _lastInput = Engine.InputManager.GetKeysState();
        }

        public void Draw(GameTime gameTime)
        {
            Engine.Display.Add(_up);
            Engine.Display.Add(_left);
            Engine.Display.Add(_right);
            Engine.Display.Add(_down);

            Engine.Display.DrawText(new PointF(20, 220), _rightMessage, Engine.DefaultFont, Color.Yellow);

            if(Engine.InputManager.SupportAccelerometer)
                Engine.Display.DrawText(new PointF(20, 240), string.Format("X={0:0.000}\r\nY={1:0.000}\r\nZ={2:0.000} (gravity)",_lastGVector.X, _lastGVector.Y, _lastGVector.Z), Engine.DefaultFont, Color.White);
            else
                Engine.Display.DrawText(new PointF(20, 240), "Accelerometer Not Supported", Engine.DefaultFont, Color.White);
        }

        public bool CanExit()
        {
            return true;
        }

        public IGameEngine Engine {get; set;}
    }
}
